﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DBA.Data;
using DBA.Data.ADO;
using DBA.Bll;
using System.Data;
using DBA.Common;

namespace DBA.Web.PaperManage
{
    public partial class PaperList : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack) return;
            ddlBind();
            Bind(1, null);
        }



        protected void linkBtnSearch_Click(object sender, EventArgs e)
        {
            IDictionary<String, String> dict = new Dictionary<String, String>();
            dict.Add("ps_PaperName", txtps_Name.Text.Trim());
            dict.Add("ps_Type", ddl_ps_Type.SelectedValue);
            dict.Add("ps_Class", txtps_Class.Text.Trim());
            dict.Add("ps_PublicationName", txtps_PublicationName.Value.Trim());
            dict.Add("ps_BeginTime", txtps_BeginTime.Value.Trim());
            dict.Add("ps_EndTime", txtps_EndTime.Value.Trim());
            dict.Add("ps_Person", txtps_Person.Value.Trim());
            dict.Add("ps_Sci", txtSci.Value.Trim());
            dict.Add("Xi_ID", ddlXi.SelectedValue);
            ViewState["conditions"] = dict;
            Bind(1, dict);
        }

        protected void ddlBind()
        {
            BaseCodeDDL.DdlInit(ddlXi, "Xi");//绑定系 

        }

        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            IDictionary<String, String> dict = null;
            if (ViewState["conditions"] != null)
            {
                dict = (IDictionary<String, String>)ViewState["conditions"];
            }
            Bind(e.NewPageIndex, dict);
        }

        private void Bind(int pageIdx, IDictionary<String, String> dict)
        {

            string sql = "select a.*,  ";
            sql += "attCount=(select count(1) from PorjectAttachment b where a.ps_ID=b.ps_ID) from PaperSum a  ";
            sql += " left join EmployeeInfo b on a.ps_Person1=b.Name and Person1Is=1 ";
            sql += " left join EmployeeInfo c on a.ps_Person2=c.Name and Person2Is=1 ";
            sql += " left join EmployeeInfo d on a.ps_Person3=d.Name and Person3Is=1 ";
            sql += " left join EmployeeInfo e on a.ps_Person4=e.Name and Person4Is=1 ";

            sql += " where 1=1  ";
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            dal.ManagerParameter.ClearParameter();
            if (dict != null)
            {
                if (!dict["ps_PaperName"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_PaperName like '%'+@ps_PaperName+'%'";
                    dal.ManagerParameter.AddNewInParameter("ps_PaperName", dict["ps_PaperName"].ToString());
                }
                if (!dict["ps_Type"].ToString().Equals("-1"))
                {
                    sql += " and a.ps_Type =@ps_Type";
                    dal.ManagerParameter.AddNewInParameter("ps_Type", dict["ps_Type"].ToString());
                }

                if (!dict["ps_Class"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_Class = @ps_Class";
                    dal.ManagerParameter.AddNewInParameter("ps_Class", dict["ps_Class"].ToString());
                }

                if (!dict["ps_PublicationName"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_PublicationName like '%'+@ps_PublicationName+'%'";
                    dal.ManagerParameter.AddNewInParameter("ps_PublicationName", dict["ps_PublicationName"].ToString());
                }

                if (!dict["ps_BeginTime"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_date >= @ps_BeginTime";
                    dal.ManagerParameter.AddNewInParameter("ps_BeginTime", dict["ps_BeginTime"].ToString());
                }

                if (!dict["ps_EndTime"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_date <= @sp_EndTime";
                    dal.ManagerParameter.AddNewInParameter("sp_EndTime", dict["ps_EndTime"].ToString());
                }

                if (!dict["ps_Sci"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_Sci = @ps_Sci";
                    dal.ManagerParameter.AddNewInParameter("ps_Sci", dict["ps_Sci"].ToString());
                }

                if (!dict["ps_Person"].ToString().Equals(string.Empty))
                {
                    sql += " and (a.ps_Person1 = @ps_Person or a.ps_Person2 = @ps_Person or a.ps_Person3 = @ps_Person or a.ps_Person4 = @ps_Person) ";
                    dal.ManagerParameter.AddNewInParameter("ps_Person", dict["ps_Person"].ToString());
                }

                if (!dict["Xi_ID"].ToString().Equals("-1"))
                {
                    sql += " and (b.xi_id = @xi_id or c.xi_id = @xi_id or d.xi_id = @xi_id or e.xi_id = @xi_id)";
                    dal.ManagerParameter.AddNewInParameter("xi_id", dict["Xi_ID"].ToString());
                }
            }       

            int iCount = 0;

            replData.DataSource = dal.ModifyData.GetPageDataTable("(" + sql + ")", "*", "", "ps_Type DESC,ps_date DESC,ps_Weight DESC, ps_ID DESC", pageIdx, 10, ManagerSQL.PageViewTableType.MixtureTable, ref iCount);
            replData.DataBind();
            AspNetPager1.RecordCount = iCount;

        }

        protected void linkBtnMaskSearch_Click(object sender, EventArgs e)
        {
            //IDictionary<String, String> dict = new Dictionary<String, String>();
            //dict.Add("UserName", txtMaskMemberId.Text.Trim());
            //ViewState["conditions"] = dict;
            //Bind(1, dict);
        }

        protected void linkBtnDeleSelect_Click(object sender, EventArgs e)
        {
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            if (!String.IsNullOrEmpty(hidDeleString.Value))
            {
                string value = hidDeleString.Value;
                string sql = "delete from [PaperSum] where ps_ID in (" + value + ");";
                dal.ExecuteNonQuery(sql);
                if (String.IsNullOrEmpty(dal.ErrorMessage))
                {
                    Bind(1, null);
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.correct('数据删除成功');", true);
                }
                else
                {
                    ScriptManager.RegisterStartupScript(this, base.GetType(), "deleOk" + System.Guid.NewGuid().ToString(), "parent.alertMsg.error('数据删除失败');", true);
                }
            }
        }

        protected void lnkDownload_Click(object sender, EventArgs e)
        {
            IDictionary<String, String> dict = new Dictionary<String, String>();
            dict.Add("ps_PaperName", txtps_Name.Text.Trim());
            dict.Add("ps_Type", ddl_ps_Type.SelectedValue);
            dict.Add("ps_Class", txtps_Class.Text.Trim());
            dict.Add("ps_PublicationName", txtps_PublicationName.Value.Trim());
            dict.Add("ps_BeginTime", txtps_BeginTime.Value.Trim());
            dict.Add("ps_EndTime", txtps_EndTime.Value.Trim());
            dict.Add("ps_Sci", txtSci.Value.Trim());
            dict.Add("ps_Person", txtps_Person.Value.Trim());
            dict.Add("Xi_ID", ddlXi.SelectedValue);
            ViewState["conditions"] = dict;



            string sql = "select ps_PaperName as 论文名称,ps_PublicationName 期刊名称,ps_EnglishName 英文名称,'会议类型'=case ps_type when 1 then '会议论文' else '期刊论文' end,ps_Class 级别,ps_Issue 期号,ps_addressPerson 通讯作者,ps_date 发表时间,ps_Sci 收录类型,ps_Weight 权重,ps_Person1 第一作者,ps_Person2 第二作者,ps_Person3 第三作者,ps_Person4 第四作者,ps_Demo 备注  ";
            sql += " from PaperSum a  ";
            sql += " left join EmployeeInfo b on a.ps_Person1=b.Name and Person1Is=1 ";
            sql += " left join EmployeeInfo c on a.ps_Person2=c.Name and Person2Is=1 ";
            sql += " left join EmployeeInfo d on a.ps_Person3=d.Name and Person3Is=1 ";
            sql += " left join EmployeeInfo e on a.ps_Person4=e.Name and Person4Is=1 ";
            sql += " where 1=1  ";
            DataAccessLibrary dal = CommonQuery.DataAccessLibrary;
            dal.ManagerParameter.ClearParameter();
            if (dict != null)
            {
                if (!dict["ps_PaperName"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_PaperName like '%'+@ps_PaperName+'%'";
                    dal.ManagerParameter.AddNewInParameter("ps_PaperName", dict["ps_PaperName"].ToString());
                }
                if (!dict["ps_Type"].ToString().Equals("-1"))
                {
                    sql += " and a.ps_Type =@ps_Type";
                    dal.ManagerParameter.AddNewInParameter("ps_Type", dict["ps_Type"].ToString());
                }

                if (!dict["ps_Class"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_Class = @ps_Class";
                    dal.ManagerParameter.AddNewInParameter("ps_Class", dict["ps_Class"].ToString());
                }

                if (!dict["ps_PublicationName"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_PublicationName like '%'+@ps_PublicationName+'%'";
                    dal.ManagerParameter.AddNewInParameter("ps_PublicationName", dict["ps_PublicationName"].ToString());
                }

                if (!dict["ps_BeginTime"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_date >= @ps_BeginTime";
                    dal.ManagerParameter.AddNewInParameter("ps_BeginTime", dict["ps_BeginTime"].ToString());
                }

                if (!dict["ps_EndTime"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_date <= @sp_EndTime";
                    dal.ManagerParameter.AddNewInParameter("sp_EndTime", dict["ps_EndTime"].ToString());
                }

                if (!dict["ps_Sci"].ToString().Equals(string.Empty))
                {
                    sql += " and a.ps_Sci = @ps_Sci";
                    dal.ManagerParameter.AddNewInParameter("ps_Sci", dict["ps_Sci"].ToString());
                }

                if (!dict["ps_Person"].ToString().Equals(string.Empty))
                {
                    sql += " and (a.ps_Person1 = @ps_Person or a.ps_Person2 = @ps_Person or a.ps_Person3 = @ps_Person or a.ps_Person4 = @ps_Person) ";
                    dal.ManagerParameter.AddNewInParameter("ps_Person", dict["ps_Person"].ToString());
                }
                if (!dict["Xi_ID"].ToString().Equals("-1"))
                {
                    sql += " and (b.xi_id = @xi_id or c.xi_id = @xi_id or d.xi_id = @xi_id or e.xi_id = @xi_id)";
                    dal.ManagerParameter.AddNewInParameter("xi_id", dict["Xi_ID"].ToString());
                }
            }
            DataTable dt = dal.ExecuteFillDataTable(sql);
            NPOIHelper.ExportByWeb(dt, string.Empty, "论文信息" + new Random().Next().ToString() + ".xls");
        }


    }
}